package com.android.thelastnoet.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.android.thelastnoet.EditActivity;
import com.android.thelastnoet.MainActivity;
import com.android.thelastnoet.R;
import com.android.thelastnoet.bean.Note;

import java.util.Date;
import java.util.List;


public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    private List<Note> noteList;
    private View view;
    private Context mContext;

    private OnRecyclerViewItemClickListener mOnItemClickListener;
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener;

    public NoteAdapter(List<Note> noteList, Context mContext) {
        this.noteList = noteList;
        this.mContext = mContext;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Note) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemLongClickListener.onItemLongClick(v, (Note) v.getTag());
        }
        return true;
    }

    public void setNoteList(List<Note> noteList) {
        this.noteList = noteList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Note note);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(View view, Note note);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        /*加载布局中的控件实例*/
        public CardView cardView;
        public ImageView imageView;
        public TextView title;
        public TextView subContent;

        public TextView createTime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.cardView = itemView.findViewById(R.id.card_view);
            this.title = itemView.findViewById(R.id.note_title);
            this.subContent = itemView.findViewById(R.id.note_subContent);
            this.createTime = itemView.findViewById(R.id.note_createTime);
        }

    }

    @NonNull
    @Override
    public NoteAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        /*创建ViewHolder 加载布局*/
        view = LayoutInflater.from(mContext).inflate(R.layout.item_note, parent, false);
        ViewHolder holder = new ViewHolder(view);
        /*注册点击事件*/
        view.setOnClickListener(this);
//        view.setOnLongClickListener(this);
        return holder;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull NoteAdapter.ViewHolder holder, int position) {
        /*对RecyclerView子项的数据进行赋值*/
        Note note = noteList.get(position);
        String title = note.getTitle();
        String content = note.getContent();
        String createTime = note.getCreateTime();
        String subContent = "";
        if (TextUtils.isEmpty(title)) {
            int i, j;
            for (i = 0; i < content.length(); i++) {
                if (content.charAt(i) == '\n') {
                    break;
                }
            }
            title = content.substring(0, i);
            if (i < content.length()){
                for (j = i + 1; j < content.length(); j++) {
                    if (content.charAt(j) == '\n') {
                        break;
                    }
                }
                subContent = content.substring(i + 1, j);
            }else {
                view.findViewById(R.id.note_subContent).setVisibility(View.GONE);
            }
        } else {
            int i;
            for (i = 0; i < content.length(); i++) {
                if (content.charAt(i) == '\n') {
                    break;
                }
            }
            subContent = content.substring(0, i);
        }
        //如果是今年新建的便签的话，没必要显示年份
        String year = String.valueOf(new Date().getYear());
        if (createTime.contains(year)){
            createTime = createTime.replace(year,"");
        }
/*        if (createTime.contains("2020年")) {
            createTime = createTime.replace("2020年", "");
        }*/
        holder.itemView.setTag(note);
        holder.title.setText(title);
        holder.subContent.setText(subContent);
        holder.createTime.setText(createTime);
    }

    @Override
    public int getItemCount() {
        Log.d("Adapter", noteList.size() + "");
        return noteList.size();
    }

}
